package com.msg.xml;

import java.util.ArrayList;

/**
 * This class gets the seats information from XML file and save it into database
 * @author jeson9603
 *
 */
public class Seats_Manipulator extends Table_Manipulator {

	private ArrayList<String> eventCode;
	private ArrayList<String> priceLevel;
	private ArrayList<ArrayList<String>> seatsTable;
	
	public Seats_Manipulator(Property prop){
		super(prop);
		eventCode = new ArrayList<String>();
		priceLevel = new ArrayList<String>();
		seatsTable = new ArrayList<ArrayList<String>>();
	}
	
	/**
	 * get the seats information from the SOLD DETAIL table
	 * including the ticket_type, price level and sold seats number
	 */
	@Override
	protected String getSpecificData() {
		//first locate the rows with the seats information
		XMLhandler.findNodeByTag("***       SOLD DETAIL");
		XMLhandler.findNodeByTag("EVENT");
		XMLhandler.moveCursorForward();
		while(true) {
			ArrayList<String> content = XMLhandler.getDataList();
			if(content.get(0).compareTo("") == 0)
				break;
			if(content.get(1).compareTo(" -------->") == 0)
				continue;
			eventCode.add(content.get(0));
			priceLevel.add(content.get(1));
			ArrayList<String> seats = new ArrayList<String>();
			for(int i = 0; i < prop.getTICKETLEVEL_NUM(); i++) {
				seats.add(content.get(4+i));
			}
			seatsTable.add(seats);
		}
		return null;
	}

	/**
	 * first check if there are new ticket types or price levels,
	 * if they are new, inserting new record into database.
	 * Then update the new seats information into price code table(if not exist, add one)
	 */
	@Override
	protected String update() {
		Table_Manipulator tic = new TicketType_Manipulator(prop);
		Table_Manipulator prc = new PriceLevel_Manipulator(prop);
		TicketGroup_Manipulator tgroup = new TicketGroup_Manipulator(prop);
		Table_Manipulator sts = new SoldSeats_Manipulator(prop);
		int length = eventCode.size();
		for(int i = 0; i < length; i++) {
			tic.pushData(eventCode.get(i));
			prc.pushData(priceLevel.get(i));
			sts.pushData(seatsTable.get(i));
			String ticketGroup[] = {eventCode.get(i), priceLevel.get(i)};
			tgroup.pushData(ticketGroup);
			
			tic.update();
			prc.update();
			tgroup.update();
			sts.update();
		}
		return null;
	}

	@Override
	protected void pushData(Object o) {
		// TODO Auto-generated method stub

	}

}
